package com.woniuxy.qiantai.config;


import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class CrossOriginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        //在响应头里加上允许跨域访问
        HttpServletResponse httpServletResponse = (HttpServletResponse)response;
        //httpServletResponse.addHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.addHeader("Access-Control-Allow-Origin", ((HttpServletRequest) request).getHeader("Origin"));

        //允许跨域携带cookie等
        httpServletResponse.addHeader("Access-Control-Allow-Credentials", "true");

        //允许自定义请求头
        httpServletResponse.addHeader("Access-Control-Allow-Headers","*");

        //允许非get或post的请求方式
        httpServletResponse.addHeader("Access-Control-Allow-Methods", "*");

        //放行
        chain.doFilter(request,response);
    }
}
